package org.xxx.epms.admin.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.xxx.epms.common.service.RedisService;
import org.xxx.epms.admin.service.AdminCacheService;
import org.xxx.epms.admin.models.AdminPermission;
import org.xxx.epms.admin.models.Admin;

import java.util.List;

@Service
public class AdminCacheServiceImpl implements AdminCacheService {

    @Autowired
    private RedisService redisService;
    @Value("${redis.database}")
    private String REDIS_DATABASE;
    @Value("${redis.expire.common}")
    private Long REDIS_EXPIRE;
    @Value("${redis.key.admin}")
    private String REDIS_KEY_ADMIN;
    @Value("${redis.key.adminPermissionList}")
    private String REDIS_KEY_ADMIN_PERMISSION_LIST;

    private String getAdminCacheKey(String adminCode) {
        return REDIS_DATABASE + ":" + REDIS_KEY_ADMIN + ":" + adminCode;
    }

    @Override
    public Admin getAdmin(String adminCode) {
        return (Admin) redisService.get(this.getAdminCacheKey(adminCode));
    }

    @Override
    public void setAdmin(Admin admin) {
        redisService.set(this.getAdminCacheKey(admin.getAdminCode()), admin, REDIS_EXPIRE);
    }

    @Override
    public void delAdmin(String adminCode) {
        redisService.del(this.getAdminCacheKey(adminCode));
    }

    private String  getAdminPermissionListCacheKey(int adminId) {
        return REDIS_DATABASE + ":" + REDIS_KEY_ADMIN_PERMISSION_LIST + ":" + adminId;
    }
    @Override
    public List<AdminPermission> getAdminPermissionList(int adminId) {
        return (List<AdminPermission>) redisService.get(this.getAdminPermissionListCacheKey(adminId));
    }

    @Override
    public void setAdminPermissionList(int adminId, List<AdminPermission> adminPermissionsList) {
        redisService.set(this.getAdminPermissionListCacheKey(adminId), adminPermissionsList, REDIS_EXPIRE);
    }

    @Override
    public void delAdminPermissionList(int adminId) {
        redisService.del(this.getAdminPermissionListCacheKey(adminId));
    }

}
